﻿using System.Text.RegularExpressions;
using System.Web;

namespace yiCommerce.Core
{
    public class KeepLinkOutPut 
    {
        //public string _input;

        //public KeepLinkOutPut(string input)
        //{
        //    this._input = input;
        //}


        static string CapText(Match m)
        {
            // Get the matched string.
            string x = m.ToString();
            // If the first char is lower case...
            return "<a target=\"_blank\" href=\"" + x + "\">" + x + "</a>";
        }

        public static string ToHtmlString(string input)
        {
            var tt = HttpUtility.HtmlEncode(input);
            //var regex = new Regex("(&lt;a)(.*)(href=&quot;)(.*)(&quot;&gt;)");
            var regex = new Regex(@"(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([=a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?");
          
            string result = regex.Replace(tt, new MatchEvaluator(CapText));

            //            var s1 = regex.Replace(tt, "<a href=\"$1\">$1</a>");
            return result;
            //  .Replace("&quot;&gt;", "\">")
            // .Replace("&lt;/a&gt;", "</a>");

            //return tt.Replace("&lt;a href=&quot;", "<a href=\"")
            //    .Replace("&quot;&gt;", "\">")
            //    .Replace("&lt;/a&gt;", "</a>");
        }
    }
}